$("document").ready(function(e) {

	gallery();

	mail = {
		ime: '',
		email: '',
		sub: '',
		text: '',
		genCheck: '',
		answered: ''
	};

	var doc_height = $(document).height();
	$("body").css({
		"min-height": doc_height + "px"
	});
	$(".border").css({
		"min-height": doc_height - 250 + "px"
	});

	$(".menu ul li a.menu-active").on("click", function() {
		return false;
	});
	toTop();
	getPitanje();

	$("#posalji").on("click", function(e) {
		e.preventDefault();
		e.stopPropagation();
		mail.ime = $("#ime").val();
		mail.email = $("#email").val();
		mail.sub = $("#subject").val();
		mail.text = $("#poruka").val();
		mail.answered = parseInt($(".test").val());
		if (checkMail()) {
			Send();
		}
	});

	$("#send").on("click", function(e) {
		e.preventDefault();
		e.stopPropagation();
		mail.ime = $("#ime").val();
		mail.email = $("#email").val();
		mail.sub = $("#subject").val();
		mail.text = $("#poruka").val();
		mail.answered = parseInt($(".test").val());
		if (checkMail()) {
			SendEng();
		}
	});

});

function checkMail() {
	var cnt = 0;
	$(".con-form input, .con-form textarea").css({
		'border': '1px solid #181818'
	});
	$(".mail-error").fadeOut("fast");
	if (mail.ime == null || mail.ime == "" || mail.ime == " ") {
		errorCss('#ime');
		cnt = 1;
	}
	if (provjera_emaila() == false) {
		errorCss('#email');
		cnt = 1;
	}
	if (mail.sub == null || mail.sub == "" || mail.sub == " ") {
		errorCss('#subject');
		cnt = 1;
	}
	if (mail.text == null || mail.text == "" || mail.text == " ") {
		errorCss('#poruka');
		cnt = 1;
	}
	if (checkPitanje() == false) {
		errorCss('.test');
		cnt = 1;
	}
	if (cnt == 0) {
		return true;
	} else {
		return false;
	}
}

function provjera_emaila() {
	if (mail.email == null || mail.email == "" || mail.email == " ") {
		return false;
	}
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if (!emailReg.test(mail.email)) {
		return false;
	} else {
		return true;
	}
}

function Send() {
	$.ajax({
		type: "POST",
		url: 'inc/mailer.php',
		data: {
			OBJ: mail
		},
		success: function(obj) {
			if (parseInt(obj) == 0) {
				$(".mail-error").html('Greška: pošaljite mail na <a href="mailto:hello@i1-creative.com">hello@i1-creative.com</a>');
				$(".mail-error").fadeIn("fast");
			} else {
				$("#posalji").unbind().css({
					"color": "#f9b33a"
				});
				$(".mail-error").html('E-mail poslan !');
				$(".mail-error").fadeIn("fast");
			}
		}
	});
}

function SendEng() {
	$.ajax({
		type: "POST",
		url: '../inc/mailer.php',
		data: {
			OBJ: mail
		},
		success: function(obj) {
			if (parseInt(obj) == 0) {
				$(".mail-error").html('Error: send mail to <a href="mailto:hello@i1-creative.com">hello@i1-creative.com</a>');
				$(".mail-error").fadeIn("fast");
			} else {
				$("#send").unbind().css({
					"color": "#f9b33a"
				});
				$(".mail-error").html('E-mail sent !');
				$(".mail-error").fadeIn("fast");
			}
		}
	});
}

function checkPitanje() {
	if (mail.genCheck == mail.answered) {
		return true;
	} else {
		return false;
	}
}

function errorCss(selector) {
	$(selector).css({
		'border': '1px solid #f9b33a'
	});
	$(".mail-error").fadeIn("fast");
}

function getPitanje() {
	var a = Math.ceil(Math.random() * 15);
	var b = Math.ceil(Math.random() * 15);
	$("#provjera").attr("placeholder", 'Sigurnosno pitanje: koliko je ' + a + '+' + b + '?')
	$("#security").attr("placeholder", 'Security question: How much is ' + a + '+' + b + '?')
	mail.genCheck = parseInt(a + b);
}

function toTop() {
	// hide #back-top first
	$("#back-top").hide();

	// fade in #back-top
	$(function() {
		$(window).scroll(function() {
			if ($(this).scrollTop() > 400) {
				$('#back-top').fadeIn();
			} else {
				$('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		$('#back-top a').click(function() {
			$('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	});
}

function gallery() {
	var w = $(window).width();
	var h = $(window).height();
	$('#gallery-wrap').css({
		'height': h + 'px'
	});

	$('.gallery-img-holder').css({
		'max-height': h - 50 + 'px',
		'max-width': w - 100 + 'px'
	});

	$('.gallery-img ').on('mouseup', function() {
		var bg = $(this).css('background-image');
		bg = bg.replace('url("', '').replace('")', '');
		$('#gallery-wrap').fadeIn(300, function() {
			$('.gallery-img-holder').attr('src', bg);
		});
	});
	$('.gallery-img-holder').on('mouseup', function(e) {
		e.stopPropagation();
	});
	$('#gallery-wrap').on('mouseup', function() {
		$(this).fadeOut(300, function() {
			$('.gallery-img-holder').removeAttr('src');
		});
	});
}